﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Homework_9
{
    public partial class Form1 : Form
    {
        //本地电脑的数据库
        static string connectStr = "server=127.0.0.1;port=3306;user=root;password=10708022;database=data1;";
        //全局连接对象
        MySqlConnection mySqlConnection = new MySqlConnection(connectStr);
        //当前的索引
        int index = 1;
        // 连接数据库函数
        void getIntoSQL()
        {
            //尝试连接数据库 
            try
            {
                mySqlConnection.Open();
                button1.Text = "数据库连接成功\n\n点击开始按钮开始单词学习";
            }
            catch (Exception ex)
            {
                button1.Text = "数据库连接失败，以下为报错\n" + ex.ToString();
            }
        }
        //获取data1中rgTable表里index为i的ChineseKey和EnglishKey，分别储存在string[0]和string[1]
        string[] getKeys(int id)
        {
            string[] result = new string[2];
            try
            {
                string getKeyOrder = "SELECT ChineseKey,EnglishKey FROM rgTable WHERE id="+id;
                MySqlCommand command = new MySqlCommand(getKeyOrder, mySqlConnection);
                MySqlDataReader dataReader = command.ExecuteReader();
                while (dataReader.Read())
                {
                    result[0] = dataReader["ChineseKey"].ToString();
                    result[1] = dataReader["EnglishKey"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                button1.Text = ex.ToString();
            }
            return result;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //构建窗口时连接数据库
            getIntoSQL();
            label1.Text=("我会在你答题时为你指出对错，加油！");
        }
        //点击开始
        private void button2_Click(object sender, EventArgs e)
        { 
            button1.Text = getKeys(index)[0];
            label1.Text = "请输入中文所对应的英文单词，使用小写";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //如果正确
            if (richTextBox1.Text.Equals(getKeys(index)[1]))
            {
                if (index==10)
                {
                    button1.Text = "恭喜已经答对了所有单词！";
                }
                else
                {
                    index += 1;
                    button1.Text = getKeys(index)[0];
                    label1.Text = "正确！";
                    richTextBox1.Text = "";
                }
            }
            //如果错误
            else
            {
                button1.Text = "回答错误！";
                label1.Text = "再试一次吧，加油！";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button1.Text = getKeys(index)[0];
            label1.Text = "请输入中文所对应的英文单词，使用小写";
        }
    }
}
